<template>
  <div id="new-vas-order" class="mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb">
        <li class="breadcrumb-item ms-2">
          <router-link to="/h5/vas/orders">服务单</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">新建</li>
        <span class="ms-auto me-2" @click.stop="back()">返回</span>
      </ol>
    </nav>

    <div class="card">
      <div class="card-body">
        <dl class="row">
          <dt class="col-3 text-end">项目</dt>
          <dd class="col-9">
            <select class="form-select" v-model="productCode">
              <option value="">请选择服务项目</option>
              <option
                :value="info.productCode"
                v-for="info in extras"
                :key="info.id"
              >
                {{ info.productName }}
              </option>
            </select>
          </dd>
          <dt class="col-3 text-end">原价</dt>
          <dd class="col-9">
            <input type="text" class="form-control" v-model.number="price" />
          </dd>
          <dt class="col-3 text-end">折扣金额</dt>
          <dd class="col-9">
            <input type="text" class="form-control" v-model.number="discount" />
          </dd>
          <dt class="col-3 text-end">成本</dt>
          <dd class="col-9">
            <input type="text" class="form-control" v-model.number="cost" />
          </dd>
          <dt class="col-3 text-end">数量</dt>
          <dd class="col-9">
            <input
              type="text"
              class="form-control"
              v-model.number="count"
              readonly=""
            />
          </dd>
          <dt class="col-3 text-end">备注</dt>
          <dd class="col-9">
            <input type="text" class="form-control" v-model.trim="remark" />
          </dd>
        </dl>
      </div>
      <div class="card-body bg-info text-white py-1">行程</div>
      <table class="table table-sm">
        <thead>
          <tr>
            <th>出发</th>
            <th>到达</th>
            <th>日期</th>
            <th>航班</th>
            <th>舱位</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="flt in flights" :key="flt.id">
            <td>{{ flt.dportName }}</td>
            <td>{{ flt.aportName }}</td>
            <td>{{ flt.ddate }}</td>
            <td>{{ flt.flightNo }}</td>
            <td>{{ flt.subclass }}</td>
          </tr>
        </tbody>
      </table>
      <div class="card-body bg-info text-white py-1 align-center">
        乘客信息
          <button
            href="javascript:void(0);"
            @click.stop="addPsg()"
            class="btn btn-sm btn-primary float-end"
          >
            增加
          </button>
      </div>
      <div class="card-body">
        <template v-for="(info, index) in passengers">
          <dl class="row" :key="info.id">
            <dt class="col-3 text-end">姓名</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                placeholder="客人姓名"
                v-model.trim="info.name"
              />
            </dd>
            <dt class="col-3 text-end">证件号</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                placeholder="证件号"
                v-model.trim="info.idNo"
              />
            </dd>
            <dt class="col-3 text-end">证件类型</dt>
            <dd class="col-9">
              <select class="form-select" v-model.number="info.idType">
                <option value="1">身份证</option>
                <option value="2">护照</option>
                <option value="99">其它</option>
              </select>
            </dd>
            <dt class="col-3 text-end">常旅客号</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                placeholder="常旅客号"
                v-model.trim="info.ffpNo"
              />
            </dd>
            <dt class="col-3 text-end">联系电话</dt>
            <dd class="col-9">
              <input
                type="text"
                class="form-control"
                placeholder="联系电话"
                v-model.trim="info.mobile"
              />
            </dd>
            <dt class="col-3 text-end"></dt>
            <dd class="col-9">
              <a
                href="javascript:void(0)"
                @click.stop="deletePsg(index)"
                class="btn btn-sm btn-danger float-end"
                >删除</a
              >
            </dd>
          </dl>
        </template>
      </div>

      <div class="card-body bg-info text-white py-1">客户及付款信息</div>
      <div class="card-body">
        <dl class="row">
          <dt class="col-3 text-end">客户</dt>
          <dd class="col-9">
            <my-select-customer
              :minId="0"
              :customerId.sync="customerId"
            ></my-select-customer>
          </dd>
          <dt class="col-3 text-end">付款方式</dt>
          <dd class="col-9">
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="1"
                v-model.number="payType"
              />
              <label class="form-check-label">现金</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="2"
                v-model.number="payType"
              />
              <label class="form-check-label">信用卡</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="4"
                v-model.number="payType"
              />
              <label class="form-check-label">支票</label>
            </div>
            <div class="form-check form-check-inline">
              <input
                class="form-check-input"
                type="radio"
                value="8"
                v-model.number="payType"
              />
              <label class="form-check-label">账单</label>
            </div>
          </dd>
        </dl>
      </div>
      <div class="card-body">
        <div class="row">
          <span class="ms-auto me-auto"
            >订单总额：<b>{{ total }}</b></span
          >
        </div>
      </div>
      <div class="card-footer">
        <button
          type="button"
          class="btn btn-primary"
          @click.stop.prevent="createVasOrder(0)"
        >
          创建
        </button>
        <button
          type="button"
          class="btn btn-primary ms-2"
          @click.stop.prevent="createVasOrder(1)"
        >
          创建并继续
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { createVasOrder } from "@/api/vas.js";
import { searchFlightOrderDetail } from "@/api/flight.js";
import { searchProducts } from "@/api/product.js";
import MySelectCustomer from "@/components/my-select2-customer.vue";

export default {
  components: {
    MySelectCustomer,
  },
  data() {
    return {
      customerId: 0,

      extras: [],
      passengers: [],
      flights: [],

      productCode: "",
      productName: "",
      price: 0,
      discount: 0,
      cost: 0,
      remark: "",
      payType: 8,
      total: 0,
      flightOrderId: 0,
    };
  },
  mounted: function () {
    const id = this.$route.query.flightOrderId;
    // console.log(this.$route)
    // console.log(id)
    if (id !== undefined) {
      this.flightOrderId = parseInt(id);
      // console.log(this.flightOrderId)
      this.searchFlightOrderDetail(this.flightOrderId);
    }
    this.searchExtraServices();
  },
  watch: {
    count: function () {
      this.calc();
    },
    price: function () {
      this.calc();
    },
    discount: function () {
      this.calc();
    },
    cost: function () {
      this.calc();
    },
    productCode: function (newVal) {
      this.setExtraInfo(newVal);
      this.calc();
    },
  },
  computed: {
    count: function () {
      return this.passengers.length;
    },
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    searchFlightOrderDetail: function (id) {
      searchFlightOrderDetail(id, (jsonResult) => {
        // console.log(jsonResult)
        if (jsonResult !== null && jsonResult.id === id) {
          this.passengers.splice(0);
          for (let i = 0; i < jsonResult.passengers.length; i++) {
            const psg = jsonResult.passengers[i];
            this.passengers.push({
              name: psg.name,
              idNo: psg.idNo,
              idType: psg.idType,
              ffpNo: psg.ffpNo,
              mobile: psg.mobile,
            });
          }

          if (jsonResult.customerType === 1) {
            this.customerId = jsonResult.customer.id;
          }

          this.flights.splice(0);
          for (let i = 0; i < jsonResult.flights.length; i++) {
            this.flights.push(jsonResult.flights[i]);
          }

          console.log(this.flights);

          this.remark = jsonResult.orderDesc;
        }
      });
    },
    createVasOrder: function (next) {
      var params = {
        flightOrderId: this.flightOrderId,
        productCode: this.productCode,
        productName: this.productName,
        price: this.price,
        discount: this.discount,
        cost: this.cost,
        count: this.count,
        customerId: this.customerId,
        payType: this.payType,
        passengers: this.passengers,
        remark: this.remark,
      };

      var jsonParams = JSON.stringify(params);

      createVasOrder(jsonParams, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg);
        } else {
          this.showErrMsg("保存成功");
          if (next === 0) {
            console.log(this.productCode);
            if (this.productCode.substring(0, 2) === "10") {
              this.$router.replace("/h5/insurance/orders");
            } else {
              this.$router.replace("/h5/vas/orders");
            }
          } else if (next === 1) {
            this.reset();
          }
        }
      });
    },
    calc: function () {
      this.total = this.count * (this.price - this.discount);
    },
    addPsg: function () {
      this.passengers.push({
        name: "",
        idNo: "",
        idType: "",
        ffpNo: "",
        mobile: "",
      });
    },
    deletePsg: function (index) {
      this.passengers.splice(index, 1);
    },
    reset: function () {
      // body...
      // this.customerId = 0
      this.productCode = "";
      this.productName = "";
      this.price = 0;
      this.discount = 0;
      this.cost = 0;
      this.remark = "";
      // this.payType = 8
      this.total = 0;
    },
    setExtraInfo: function (newCode) {
      this.productName = "";
      this.price = 0;
      this.discount = 0;
      for (let i = 0; i < this.extras.length; i++) {
        const info = this.extras[i];
        if (info.productCode === newCode) {
          this.productName = info.productName;
          this.price = info.price;
          this.discount = info.discount;
          this.cost = info.cost;
        }
      }
    },
    searchExtraServices: function () {
      const params = {
        "sc.pageNo": 1,
        "sc.pageSize": 50,
      };

      searchProducts(params, (jsonResult) => {
        this.extras = jsonResult.dataList;
      });
    },
  },
};
</script>